# Definition for singly-linked list.
class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next
class Solution:
    def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]:
        dumt = ListNode(-1,head)
        p = head
        q = dumt

        for i in range(n):
            p = p.next
        while p:
            q = q.next
            p = p.next
        q.next = q.next.next
        return dumt.next